﻿#ifndef TRANS_H
#define TRANS_H

#include <QTranslator>
#include <QObject>
#include"XuanCommon_global.h"
#include<QHash>
class QQmlContext;

///
/// \brief 翻译类
/// 用于不同语言之间的翻译
///
class  XUANCOMMON_EXPORT Trans : public QTranslator
{
    Q_OBJECT
public:
    using QTranslator::QTranslator;

public: //重写以及重载
    ///
    /// \brief translate 该方法用于重写qt查找需要转义的字符串
    /// \param context tr()所属的类名
    /// \param sourceText  tr的值
    /// \param disambiguation 注释,消除歧义
    /// \param n 不为-1 返回一个适当的翻译
    /// \return 返回翻译的key
    ///
    QString translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const override;

public://注册的方法
    Q_INVOKABLE bool load(QString & lang,const QString &filePath); //加载翻译文件
    Q_INVOKABLE void loadFolder(const QString &floder);
public://变量的get以及set
    const QStringList &Languages() const;
    void setLanguages(const QStringList &);
    const QString & CurrentLang()const;
    void setCurrentLang(const QString &);
    const QString &transString()const;
public:
    ///
    /// \brief beforeUiReady 翻译文件的安装必须在界面完成之前初始化完毕
    /// \param ctx qml上下文
    /// \param folder 翻译文件路径
    ///
    void beforeUiReady(QQmlContext *ctx,const QString &folder);

    void initEnglish();
signals:
    void langLoaded(QString);
    void CurrentLanguageChanged();
    void LanguagesChanged();
    void transStringChanged();
    void folderLoad(const QString & folder);
private:
    QQmlContext *m_ctx = nullptr;
    //QHash和QMap是提供类型的功能,不同的是
    QHash<QString,QHash<QString,QString>>m_map;
    QStringList m_Languages;
    QString m_transString;
    QString m_currentLang;
};

#endif // TRANS_H
